/**
 * @(#)com.framework.web.sys.service.UserService
 *
 * Copyright (c) 2014-2018 上海天跃科技有限公司
 *
 * DESC:
 *
 */
package com.framework.web.sys.service;

import com.framework.core.constant.Globals;
import com.framework.core.util.StringUtil;
import com.framework.web.sys.domain.Resource;
import com.framework.web.sys.domain.RoleResource;
import com.framework.web.sys.repository.RoleResourceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author fusf
 * @version 1.0  2016/1/8
 */
@Service
public class RoleResourceService {


    @Autowired
    private RoleResourceRepository roleResourceRepository;

    /**
     * 保存角色资源
     * @param roleId
     * @param resourceIds
     * @return
     */
    @Transactional
    public void save(String roleId,String resourceIds) {
        List<RoleResource> roleResourceList =  roleResourceRepository.findByRoleId(roleId);
        roleResourceRepository.delete(roleResourceList);
        //roleResourceRepository.deleteByRoleId(roleId);
        if (StringUtil.isNotEmpty(resourceIds)) {
            String[] arrays = resourceIds.split(",");
            for (String resId : arrays) {
                RoleResource roleResource = new RoleResource();
                roleResource.setRoleId(roleId);
                roleResource.setResourceId(resId);
                roleResourceRepository.save(roleResource);
            }
        }
    }


    /**
     * 根据角色Id获取角色资源关联信息
     * @param roleId
     * @return
     */
    public List<RoleResource> findByRoleId(String roleId) {
        return roleResourceRepository.findByRoleId(roleId);
    }



}
